package com.sgb.kjwl.view.ui.activity.self;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.View;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.databinding.ActivityCarManagerBinding;
import com.sgb.kjwl.di.self.DaggerSelfComponent;
import com.sgb.kjwl.di.self.SelfModule;

import com.sgb.kjwl.model.entity.self.CarListEntity;
import com.sgb.kjwl.view.ui.activity.self.logistics.AddCarActivity;
import com.sgb.kjwl.view.ui.adapter.self.CarInfoAdapter;
import com.sgb.kjwl.viewmodel.self.CarListModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.decoration.RecyclerViewItemDecoration;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.List;

import javax.inject.Inject;

public class CarManagerActivity extends AppActivity {
    @Inject
    CarListModel carListModel;
    ActivityCarManagerBinding binding;
    CarInfoAdapter adapter;
    public static void start(Context context) {
        Intent intent = new Intent(context, CarManagerActivity.class);

        context.startActivity(intent);
    }
    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_car_manager);
        binding.include.rightView.setText("新增车辆");
        binding.include.titleView.setText("车辆管理");
        binding.include.rightView.setVisibility(View.VISIBLE);
        binding.include.rightView.setOnClickListener(this);
        binding.include.ivBack.setOnClickListener(this);
        if (adapter == null) {
            adapter = new CarInfoAdapter(getActivity(), null);
        }
        DividerItemDecoration divider = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(this, R.drawable.shape_list_divide));
        binding.recyclerView.addItemDecoration(divider);
        binding.recyclerView.setPullRefreshEnabled(true);
        binding.recyclerView.setLoadingMoreEnabled(false);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        binding.recyclerView.setLayoutManager(layoutManager);
        binding.recyclerView.setAdapter(adapter);

        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                carListModel.getList(false);
            }

            @Override
            public void onLoadMore() {
                carListModel.getList(true);
            }
        });
        adapter.setOnItemClick(new AdapterOnItemClick<CarListEntity>() {
            @Override
            public void onItemClick(CarListEntity carListEntity, int position) {
                Intent intent=new Intent(CarManagerActivity.this,MyCarInfoActivity.class);
                intent.putExtra("carNo",carListEntity.getNo());
                startActivity(intent);
            }
        });

    }

    @Override
    protected void init() {


    }

    @Override
    protected void onResume() {
        super.onResume();
        carListModel.getList(false);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerSelfComponent.builder().appComponent(appComponent).selfModule(new SelfModule(this)).build().inject(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.right_view:
                AddCarActivity.start(this);
                break;
        }
    }
    public void showList(List<CarListEntity> list, boolean isLast) {
        binding.recyclerView.refreshComplete();
        binding.recyclerView.loadMoreComplete();
        if (list.size() > 0) {
            adapter.setDatas(list);
            binding.emptyView.setVisibility(View.GONE);
        } else {
            adapter.setDatas(null);
            binding.emptyView.setVisibility(View.VISIBLE);
        }
    /*    if (isLast) {
            binding.recyclerView.setLoadingMoreEnabled(false);
        } else {
            binding.recyclerView.setLoadingMoreEnabled(true);
        }*/

    }
}
